package com.example.design.ddd.practice.domain.common.validator;

import com.example.design.ddd.practice.common.framework.exception.BusinessException;
import com.example.design.ddd.practice.domain.Tenantmng.repository.TenantRepository;
import com.example.design.ddd.practice.domain.Tenantmng.entity.TenantStatus;
import com.example.design.ddd.practice.domain.orgmng.repository.OrgRepository;
import com.example.design.ddd.practice.domain.orgmng.entity.OrgStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CommonValidator {
    @Autowired
    TenantRepository tenantRepository;
    private final OrgRepository orgRepository;

    @Autowired
    public CommonValidator(OrgRepository orgRepository) {
        this.orgRepository = orgRepository;
    }

    public void shouldValid(Long tenantId, Long orgId) {
        if (!orgRepository.existsByIdAndStatus(tenantId, orgId, OrgStatus.EFFECTIVE)){
            throw new BusinessException("id为'" + orgId + "'的组织不是有效组织！");
        }
    }


    public void tenantShouldValid(Long tenant) {
        if (!tenantRepository.existsByIdAndStatus(tenant, TenantStatus.EFFECTIVE)) {
            throw new BusinessException("id为'" + tenant + "'的租户不是有效租户！");
        }
    }
}